package com.ruoyi.web.controller.gd;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gd.detail.domain.bo.ZDetailBo;
import com.ruoyi.gd.detail.domain.vo.ZDetailVo;
import com.ruoyi.gd.detail.service.IZDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 发票详情
 *
 * @author ruoyi
 * @date 2024-07-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/gd/detail")
public class ZDetailController extends BaseController {

    private final IZDetailService iZDetailService;

    /**
     * 查询发票详情列表
     */
    @SaCheckPermission("gd:detail:list")
    @GetMapping("/list")
    public TableDataInfo<ZDetailVo> list(ZDetailBo bo, PageQuery pageQuery) {
        return iZDetailService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出发票详情列表
     */
    @SaCheckPermission("gd:detail:export")
    @Log(title = "发票详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ZDetailBo bo, HttpServletResponse response) {
        List<ZDetailVo> list = iZDetailService.queryList(bo);
        ExcelUtil.exportExcel(list, "发票详情", ZDetailVo.class, response);
    }

    /**
     * 获取发票详情详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("gd:detail:query")
    @GetMapping("/{id}")
    public R<ZDetailVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iZDetailService.queryById(id));
    }

    /**
     * 新增发票详情
     */
    @SaCheckPermission("gd:detail:add")
    @Log(title = "发票详情", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ZDetailBo bo) {
        return toAjax(iZDetailService.insertByBo(bo));
    }

    /**
     * 修改发票详情
     */
    @SaCheckPermission("gd:detail:edit")
    @Log(title = "发票详情", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ZDetailBo bo) {
        return toAjax(iZDetailService.updateByBo(bo));
    }

    /**
     * 删除发票详情
     *
     * @param ids 主键串
     */
    @SaCheckPermission("gd:detail:remove")
    @Log(title = "发票详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iZDetailService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
